$(function () {
    $("#provinceId").change(function () {
        $("#cityId").val("");
        $("#cityId").html("<option value=''>全部</option>");
        var pid=$("#provinceId").val();
        if (pid!="0"){
            _loadcompanycitys(pid);
        }else{
            alert('请选择所在地省份!');
        }
    });
    $("#provinceId").change(function () {
        $("#cityId").val("");
        $("#cityId").html("<option value=''>全部</option>");
        var pid=$("#provinceId").val();
        if (pid!="0"){
            _loadservicecitys(pid);
        }else{
            alert('请选择所在地省份!');
        }
    });

    //通过
    $("#auditPass").click(function () {
        $('#selectrow').val($("#requireId").val());
        $('#operateStatus').val('30');
        $('#alertflag').val('1');
        $('#popup_reason').hide();
        $('#popup_message').text('');
        $('#popup_message').append("确定要审核通过吗？");
        $('#popup_message').show();
        $('#submitbutton').show();
        $('#myModal').model('show');
    });
    $("#export").click(function(){$("#designerForm").submit();});
    //驳回
    $("#auditBack").click(function () {
        $('#selectrow').val($("#requireId").val());
        $('#operateStatus').val('40');
        $("#myModalLabel").text("提示");
        $('#alertflag').val('2');
        $('#popup_message').append("确定要审核驳回吗？");
        $('#popup_message').hide();
        $('#submitbutton').show();
        $('#popup_reason').show();
        $('#myModal').model('show');
    });
});

function _loadcompanycitys(provinceId){
    jQuery.ajax({
        url: "//"+window.location.host+'/address/getCity.action?provinceId='+provinceId,
        type: 'get',
        dataType: 'json',
        success: function(result){
            $("#cityId").empty();
            $("#cityId").append("<option value='0'>全部</option>");
            $.each(result,function(_ind,obj){
                $("#cityId").append("<option value='"+obj.id+"'>"+obj.name+"</option>");
            });
        }
    });
}

function _loadservicecitys(provinceId){
    jQuery.ajax({
        url: "//"+window.location.host+'/address/getCity.action?provinceId='+provinceId,
        type: 'get',
        dataType: 'json',
        success: function(result){
            $("#cityId").empty();
            $("#cityId").append("<option value='0'>全部</option>");
            $.each(result,function(_ind,obj){
                $("#cityId").append("<option value='"+obj.id+"'>"+obj.name+"</option>");
            });
        }
    });
}


//查找按钮
$("#search").click(function(){
//                if(numbercheck()) {
        $("#designerForm").submit();
//                }
    }
);

//项目编号格式检查
function numbercheck(){
    if(isNaN($("#searchpid").val())){
        $("#searchpid").val("");
        alert("项目ID应为数字");
        return false;
    }
    return true;
}

//弹框跳转
function popupwindow_ok() {
    var requireId = $('#selectrow').val();
    var pstatus = $('#operateStatus').val();
    var alertflag = $('#alertflag').val();
    if (pstatus == 30) {
        auditing(requireId, 30, null,alertflag);
    } else if (pstatus == 40) {
        var reason = $("#reject_reason").val();
        reason = reason.trim();
        if (reason.length == 0) {
            alert("请填写驳回原因");
            return;
        } else if (reason.length > 100) {
            alert("驳回原因最多100字");
            return;
        }
        auditing(requireId, 40, reason,alertflag);
    }
}
//审核
var lock = false;
function auditing(requireId, approvalResult, approvalNote,alertflag) {
    //表单验证通过，提交表单到服务器
    if (lock) {
        return;
    }
    lock = true;
    var url = "/productRequirement/auditing.action";
    var approvalRecordVo = {};
    approvalRecordVo.businessId = requireId;
    approvalRecordVo.approvalNote = approvalNote;
    approvalRecordVo.approvalResult = approvalResult;
    $.ajax({
        url: url,
        type: "POST",
        data: JSON.stringify(approvalRecordVo),
        contentType: 'application/json;charset=utf-8',
        dataType: "json",
        success: function (data) {
            lock = false;
            if (data.success) {
                window.location.href = '/productRequirement/listRequirementManage.action';
                if(alertflag == '1'){
                    alert("审核通过");
                }else if(alertflag == '2'){
                    alert("驳回成功");
                }
            } else {
                alert(data.info);
            }
        },
        error: function () {
            lock = false;
            alert('哎呦喂！网络不给力，再点一下吧O(∩_∩)O~');
        }
    });
}

